package com.lzy.business.controller.agent;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.IncrementApi;
import com.lzy.business.controller.BaseController;
import com.lzy.business.facade.AgentProductFacade;
import com.lzy.business.request.AgentProductRequest;
import com.lzy.business.response.AgentProductResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.dtos.ProjectDto;
import com.lzy.product.dtos.agent.AgentProductDto;
import com.lzy.product.dtos.agent.AgentProductMotorcycleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by WIN7 on 2018/4/13.
 */
@Controller
@RequestMapping("/admin/agent/product")
public class AgentProductController extends BaseController {

    @Autowired
    private AgentProductFacade agentProductFacade;

    @Autowired
    private IncrementApi incrementApi;


    @RequestMapping(value = "", method = RequestMethod.GET)
    public String productView() {
        return "agent/product/manage_list";
    }

    /**
     * 产品新增
     *
     * @return
     */
    @RequestMapping(value = "/addView", method = RequestMethod.GET)
    public String addView() {
        return "agent/product/manage_add";
    }


    /**
     * 查询代理商类型列表
     *
     * @return
     */
    @RequestMapping(value = "/typeList", method = RequestMethod.GET)
    @ResponseBody
    public Result baseProductList() {
        PageInfo<ProjectDto> pageInfo = agentProductFacade.queryBaseProductList();
        List<AgentProductMotorcycleDto> motorcycleList = agentProductFacade.queryMotorcycleList();
        Map<String, Object> map = new HashMap<>();
        map.put("baseProductList", pageInfo.getList());
        map.put("motorcycleList", motorcycleList);
        return new Result(map);
    }


    /**
     * 产品管理编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/editView/{id}", method = RequestMethod.GET)
    public String editView(Model model, @PathVariable("id") Long id) {
        AgentProductDto agentProductDto = agentProductFacade.queryAgentProductById(id);
        model.addAttribute("agentProductDto", agentProductDto);
        return "agent/product/manage_add";
    }

    @RequestMapping(value = "detailView/{id}", method = RequestMethod.GET)
    public String detailView(@PathVariable("id") Long id) {
        return "agent/product/manage_view";
    }

    /**
     * 查询产品列表数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryAgentProductList", method = RequestMethod.GET)
    @ResponseBody
    public PageBean queryAgentProductList(AgentProductRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<AgentProductDto> pageInfo = agentProductFacade.queryAgentProductList(pager, DozerUtils.map(request, AgentProductDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), AgentProductResponse.class));
        }
        return new PageBean<>();
    }

    /**
     * 新建商品
     */
    @RequestMapping(value = "/createAagntProduct", method = RequestMethod.GET)
    @ResponseBody
    public Result createAagntProduct(AgentProductDto agentProductDto) {
        //获取产品基本信息
        agentProductDto.setCreateUser(getLoginUserID());
        agentProductDto.setUpdateUser(getLoginUserID());
        agentProductFacade.createAagntProduct(agentProductDto);
        return new Result();
    }

    /**
     * 编辑商品
     */
    @RequestMapping(value = "/updateAagntProduct", method = RequestMethod.GET)
    @ResponseBody
    public Result updateAagntProduct(AgentProductDto agentProductDto) {
        //获取产品基本信息
        agentProductDto.setUpdateUser(getLoginUserID());
        agentProductFacade.updateAagntProduct(agentProductDto);
        return new Result();
    }

    /**
     * 查詢查询明細
     */
    @RequestMapping(value = "/detailView", method = RequestMethod.GET)
    public String detailView(Long id, Model model) {
        //获取产品基本信息
        AgentProductDto agentProductDto = agentProductFacade.queryAgentProductById(id);
        model.addAttribute("agentProductDto", agentProductDto);
        return "/agent/product/manage_view";
    }

    /**
     * 查詢查询明細
     */
    @RequestMapping(value = "/queryProductById", method = RequestMethod.GET)
    @ResponseBody
    public Result queryProductById(Long id) {
        //获取产品基本信息
        AgentProductDto agentProductDto = agentProductFacade.queryAgentProductById(id);
        return new Result(agentProductDto);
    }

    /**
     * 删除商品
     */
    @RequestMapping(value = "/deleteAagntProduct", method = RequestMethod.GET)
    @ResponseBody
    public Result deleteAagntProduct(Long id) {
        agentProductFacade.deleteAagntProduct(id);
        return new Result();
    }

    /**
     * 批量删除商品
     */
    @RequestMapping(value = "/deleteAagntProductList", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteAagntProductList(@RequestParam("ids") Long[] idList) {
        agentProductFacade.deleteAagntProductList(idList);
        return new Result();
    }

    @RequestMapping(value = "/incrementList", method = RequestMethod.GET)
    @ResponseBody
    public Result incrementList() {
        return new Result(agentProductFacade.incrementList());
    }
}
